<?php
$imageType = 'png';
$w = $_GET['w'];
$h = $_GET['h'];

$yMin = 0;
$yMax = $_GET['yMax'];
//$yInt = 2;

$xMin = $_GET['xMin'];
$xMax = $_GET['xMax'];
$xInt = isset($_GET['xInt']) ? $_GET['xInt'] : 1;
$vals = split(',', $_GET['vals']);
$fontSize = 8;
$wGraph = $w-20-$fontSize*4/3;
$hGraph = $h-20-$fontSize/2;
$xGraph = 20;
$yGraph = $h-20;
$wBar = round($xInt*$wGraph/($xMax-$xMin)*2/3);
$xLabelInt = round(9/3*$fontSize / $wBar);
$yInt = ceil($fontSize/($hGraph/$yMax));

$im = imagecreatetruecolor($w, $h);

$white = imagecolorallocate($im, 255, 255, 255);
$axisColor = imagecolorallocate($im, 100, 100, 100);
$lineColor = imagecolorallocate($im, 220, 220, 220);
$barColor = imagecolorallocate($im, 77, 137, 249);
imagesetstyle($im, array(IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, $lineColor, $lineColor));

imagefill($im, 0, 0, $white);

imageline($im, $xGraph, $yGraph, $xGraph+$wGraph, $yGraph, $axisColor);
imageline($im, $xGraph, $yGraph, $xGraph, $yGraph-$hGraph, $axisColor);

for ($y = $yMin; $y <= $yMax; $y += $yInt) {
	$yCoord = $yGraph-round(($y-yMin)/($yMax-$yMin)*$hGraph);
	imageline($im, $xGraph-2, $yCoord, $xGraph, $yCoord, $axisColor);
	if ($y != $yMin)
		imageline($im, $xGraph+1, $yCoord, $yGraph+$wGraph, $yCoord, IMG_COLOR_STYLED);
	$xFont = strlen($y) == 1 ? 10 : 5;
	imagettftext($im, $fontSize, 0, $xFont, $yCoord+$fontSize/2, $axisColor, './arial.ttf', $y);
}


for ($x = $xMin; $x <= $xMax; $x += $xInt) {
	$xCoord = $xGraph+round(($x-$xMin+$xInt/2)/($xMax-$xMin+$xInt)*$wGraph);
	imageline($im, $xCoord, $yGraph+2, $xCoord, $yGraph, $axisColor);
	if (($x-$xMin) % $xLabelInt == 0)
		imagettftext($im, $fontSize, 0, $xCoord-2*$fontSize*2/3, $yGraph+$fontSize+4, $axisColor, './arial.ttf', $x);
	if ($vals[($x-$xMin)/$xInt] != $yMin)
	imagefilledrectangle($im, $xCoord-$wBar/2, $yGraph-1, $xCoord+$wBar/2, $yGraph-($vals[($x-$xMin)/$xInt]-$yMin)/($yMax-$yMin)*$hGraph, $barColor);
}

if ($imageType == 'png') {
  header('Content-type: image/png');
  imagepng($im);
}
imagedestroy($im);
?>
